package com.generator.database;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.domain.Sort.Order;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.TransactionCallback;
import org.springframework.transaction.support.TransactionTemplate;

import com.alibaba.fastjson.JSONObject;
import com.generator.database.genrepository.GenericConfigMapper;
import com.generator.database.genrepository.UserMapper;
import com.generator.database.model.GenericConfig;
import com.generator.database.model.User;

import lombok.extern.slf4j.Slf4j;
/**
 * 测试sharding-JDBC
 * 2018-2-11 14:45:13
 * @author chenjinlong
 */
@Slf4j
@RunWith(SpringJUnit4ClassRunner.class) 
@ContextConfiguration(locations = {"classpath:applicationContext-sharding-01.xml"})
public class ShardingJDBCTest01 {
	
	@Autowired
	private UserMapper userMapper;
	@Autowired
	private GenericConfigMapper genericConfigMapper;
	@Autowired
	private TransactionTemplate transactionTemplate;
	
	
	
	//tb_generic_config单库单表
	@Test
	public void GenericConfig_insertSelectiveReturnPrimaryKey() {
		GenericConfig genericConfig = new GenericConfig();
		genericConfig.setConfigCode("0123");
		genericConfig.setConfigText("AAA");
		genericConfig.setConfigNum(new BigDecimal(100));
		genericConfig.setMemo("insertSelectiveReturnPrimaryKey");
		genericConfig.setCreateTime(new Date());
		genericConfig.setUpdateTime(new Date());
		genericConfig.setIsDeleted(false);
		
		int resultCount = genericConfigMapper.insertSelectiveReturnPrimaryKey(genericConfig);
		log.info("#########insertSelectiveReturnPrimaryKey:{},pkId:{}",resultCount,genericConfig.getId());
	}
	
	@Test
	public void GenericConfig_insertIgnoreSelective() {
		GenericConfig genericConfig = new GenericConfig();
		genericConfig.setConfigCode("12345");
		genericConfig.setConfigText("AAA");
		genericConfig.setConfigNum(new BigDecimal(100));
		genericConfig.setMemo("insertIgnoreSelective");
		genericConfig.setCreateTime(new Date());
		genericConfig.setUpdateTime(new Date());
		genericConfig.setIsDeleted(false);
		
		int resultCount = genericConfigMapper.insertIgnoreSelective(genericConfig);
		log.info("#########insertIgnoreSelective:{}",resultCount);
	}
	
	@Test
	public void GenericConfig_selectSelective() {
		GenericConfig queryGenericConfig = new GenericConfig();
		queryGenericConfig.setConfigCode("0123");
		queryGenericConfig.setIsDeleted(false);
		
		Long totalCount = genericConfigMapper.selectCountSelective(queryGenericConfig);
		log.info("#########genericConfig:{}",totalCount);
		
		PageRequest pageRequest1 = new PageRequest(0,3);
		log.info("#########genericConfig:{}",JSONObject.toJSONString(genericConfigMapper.selectPageSelective(queryGenericConfig,pageRequest1),true));
		
		PageRequest pageRequest2 = new PageRequest(0,10,new Sort(new Order(Direction.DESC,"id")));
		log.info("#########genericConfig:{}",JSONObject.toJSONString(genericConfigMapper.selectPageSelective(queryGenericConfig,pageRequest2),true));
		
		PageRequest pageRequest3 = new PageRequest(0,10,new Sort(new Order(Direction.DESC,"id"),new Order(Direction.ASC,"create_time")));
		log.info("#########genericConfig:{}",JSONObject.toJSONString(genericConfigMapper.selectPageSelective(queryGenericConfig,pageRequest3),true));
		
		
		//spring-data-commons，高版本，如2.1.5.RELEASE
//		PageRequest pageRequest2 = PageRequest.of(0,20,Sort.by(new Order(Direction.ASC,"id")));
//		log.info("#########genericConfig:{}",JSONObject.toJSONString(genericConfigMapper.selectPageSelective(queryGenericConfig,pageRequest2),true));
//		
//		PageRequest pageRequest3 = PageRequest.of(0,20,Sort.by(new Order(Direction.DESC,"id"),new Order(Direction.ASC,"create_time")));
//		log.info("#########genericConfig:{}",JSONObject.toJSONString(genericConfigMapper.selectPageSelective(queryGenericConfig,pageRequest3),true));
	}
	
	@Test
	public void GenericConfig_selectFirstSelective() {
		GenericConfig queryGenericConfig = new GenericConfig();
		queryGenericConfig.setConfigCode("12345");
		queryGenericConfig.setIsDeleted(false);
		
		GenericConfig genericConfig = genericConfigMapper.selectFirstSelective(queryGenericConfig);
		log.info("#########genericConfig:{}",genericConfig);
	}
	
	@Test
	public void GenericConfig_selectLastSelective() {
		GenericConfig queryGenericConfig = new GenericConfig();
		queryGenericConfig.setConfigCode("0123");
		queryGenericConfig.setIsDeleted(false);
		
		GenericConfig genericConfig = genericConfigMapper.selectLastSelective(queryGenericConfig);
		log.info("#########genericConfig:{}",genericConfig);
	}
	
	@Test
	public void GenericConfig_logicDeleteByPrimaryKey() {
		int resultCount = genericConfigMapper.logicDeleteByPrimaryKey(13L);
		log.info("#########resultCount:{}",resultCount);
	}
	
	@Test
	public void GenericConfig_batchLogicDeleteByPrimaryKey() {
		List<Long> pkIds = new ArrayList<Long>();
		pkIds.add(13L);
		pkIds.add(14L);
		pkIds.add(15L);
		
		int resultCount = genericConfigMapper.batchLogicDeleteByPrimaryKey(pkIds);
		log.info("#########resultCount:{}",resultCount);
	}
	
	
	//tb_user单库 8张表
	@Test
	public void User_insertSelective() {
		//单条插入
		Date now = new Date();
		
		for(int i=1;i<10;i++) {
			User user = new User();
			user.setId(Long.valueOf(i));
			user.setUserId(Long.valueOf(1000+i));
			user.setUserName("B0- "+i+" -OK");
			user.setPhoneNo("AI- "+i+" -BU");
			user.setBirthday(now);
			user.setHeight(100+i);
			user.setInserttime(now);
			user.setUpdatetime(now);
			user.setIsDeleted(false);
			
			int resultCount = userMapper.insertSelective(user);
			log.info("#########insertIgnoreSelective:{}",resultCount);
		}
	}
	
	@Test
	public void User_insertIgnoreSelective() {
		//insertIgnore单条插入
		Date now = new Date();
		
		User user = new User();
		user.setId(11L);
		user.setUserId(2002L);
		user.setUserName("B0- "+110+" -OK");
		user.setPhoneNo("AI- "+110+" -BU");
		user.setBirthday(now);
		user.setHeight(100);
		user.setInserttime(now);
		user.setUpdatetime(now);
		user.setIsDeleted(false);
		
		int resultCount = userMapper.insertIgnoreSelective(user);
		log.info("#########insertIgnoreSelective:{}",resultCount);
	}
	@Test
	public void User_batchInsert() {
		//多条插入
		List<User> users = new ArrayList<User>();
		
		Date now = new Date();
		for(int i=21;i<30;i++) {
			User user = new User();
			user.setId(Long.valueOf(i));
			user.setUserId(Long.valueOf(1000+i));
			user.setUserName("B0- "+i+" -OK");
			user.setPhoneNo("AI- "+i+" -BU");
			user.setBirthday(now);
			user.setHeight(100+i);
			user.setInserttime(now);
			user.setUpdatetime(now);
			user.setIsDeleted(false);
			
			users.add(user);
		}
		
		int resultCount = userMapper.batchInsert(users);
		log.info("#########batchInsert:{}",resultCount);
	}
	
	@Test
	public void User_selectSelective() {
		User queryUser = new User();
		queryUser.setUserId(1024L);
		queryUser.setIsDeleted(false);
		
		Long totalCount = userMapper.selectCountSelective(queryUser);
		log.info("#########genericConfig:{}",totalCount);
		
		PageRequest pageRequest1 = new PageRequest(0,3);
		log.info("#########genericConfig:{}",JSONObject.toJSONString(userMapper.selectPageSelective(queryUser,pageRequest1),true));
		
		PageRequest pageRequest2 = new PageRequest(0,10,new Sort(new Order(Direction.DESC,"id")));
		log.info("#########genericConfig:{}",JSONObject.toJSONString(userMapper.selectPageSelective(queryUser,pageRequest2),true));
		
		PageRequest pageRequest3 = new PageRequest(0,5,new Sort(new Order(Direction.DESC,"id"),new Order(Direction.ASC,"create_time")));
		log.info("#########genericConfig:{}",JSONObject.toJSONString(userMapper.selectPageSelective(queryUser,pageRequest3),true));
	}
	
	@Test
	public void User_selectFirstSelective() {
		User queryUser = new User();
		queryUser.setUserId(1024L);
		queryUser.setIsDeleted(false);
		
		User user = userMapper.selectFirstSelective(queryUser);
		log.info("#########genericConfig:{}",user);
	}
	
	@Test
	public void User_selectLastSelective() {
		User queryUser = new User();
		queryUser.setUserId(1024L);
		queryUser.setIsDeleted(false);
		
		User user = userMapper.selectLastSelective(queryUser);
		log.info("#########genericConfig:{}",user);
	}
	
	@Test
	public void User_updateByPrimaryKeySelective() {
		User updateUser = new User();
		updateUser.setId(26L);
		updateUser.setUserName("chenjinlong");
		updateUser.setPhoneNo("150262200345");
		updateUser.setBirthday(new Date());
		updateUser.setHeight(178);
		
		log.info("#########genericConfig:{}",userMapper.updateByPrimaryKeySelective(updateUser));
	}
	
	
	
	@Test
	public void transaction() {
		//测试一个db范围的事务
		transactionTemplate.execute(new TransactionCallback<Integer>() {
			@Override
			public Integer doInTransaction(TransactionStatus status) {
				//
				GenericConfig genericConfig = new GenericConfig();
				genericConfig.setConfigCode("APPA");
				genericConfig.setConfigText("AAA");
				genericConfig.setConfigNum(new BigDecimal(100));
				genericConfig.setMemo("insertSelectiveReturnPrimaryKey");
				genericConfig.setCreateTime(new Date());
				genericConfig.setUpdateTime(new Date());
				genericConfig.setIsDeleted(false);
				genericConfigMapper.insertSelectiveReturnPrimaryKey(genericConfig);
				
				User user = new User();
				user.setId(10009L);
				user.setUserId(33L);
				user.setUserName("BOOK");
				user.setPhoneNo("901234");
				user.setBirthday(new Date());
				user.setHeight(100);
				user.setInserttime(new Date());
				user.setUpdatetime(new Date());
				user.setIsDeleted(false);
				userMapper.insertSelective(user);
				
				//Integer.parseInt("9L");
				return 1;
			}
		});
	}
	
	
}